//*********************************************
//ADRIAN SALAZAR && JUSTIN MICHALE SCHWARTZBECK
//HOMEWORK 2
//FEB. 23, 2012
//
//Worker: This is the worker class that will request to
//	the server to do work over a range of password
//	possibilities. 
//
//*********************************************


#include "lsp.h"
#include "lex.h"
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <openssl/sha.h>
#define MAX_SIZE 4096

#define min 'a'
#define max 'z'

int main(int argc, char** argv) 
{
	int port = 8016;
	char * ip = "127.0.0.1";
	
	
	//Initialize Connection
	if(argc > 1)
	{
		ip = strtok(argv[1], ":");
		if(ip != NULL)
		{
			port = atoi(strtok(NULL, " "));
			//strtok(NULL, " ");//argv[1]); 
		}
		else
		{
			printf("The correct format is host:port\n");
			return -1;
		}
	}
	
	//create new worker client
	lsp_client * worker = lsp_client_create(ip, port);
	
	//send initial request
	unsigned char init_msg[] = "j";
	lsp_client_write(worker, (uint8_t *) init_msg, strlen((const char *)init_msg));
	
	
	do
	{
		//printf("waiting for a job\n");
		//Create a new set of variables each time
		char * hash;
		char * lower;
		char * upper;
		uint8_t buffer[4096];
		memset(buffer, 0, 4096);
	
		int bytes_read = lsp_client_read(worker, (uint8_t *) buffer);
		if(bytes_read > 0)
		{
			hash = strtok((char *)buffer, ":");
			//hash = hash_to_str((unsigned char*)hash);
			lower = strtok(NULL, ":");
			upper = strtok(NULL, "\n");
			//printf("%s\n", hash);
			//printf("%s\n", lower);
			//printf("%s\n", upper);
			//sleep(5);
			//Read the work assigned by the server
		
			//sleep(4);
			char inc;
			bool found = false;
			do
			{	inc = 0;
				char * new_hash = new char[20];
				memset(new_hash, 0, 20);
				//Hash the different combination of passwords
				SHA1((const unsigned char*)lower, strlen((const char *)lower), (unsigned char *)new_hash);
				new_hash = hash_to_str((unsigned char*)new_hash);	

				//printf("%s, %s\n", hash, new_hash);
				//Compare the results
				if(strcmp((const char *)hash, (const char *)new_hash) == 0)
				{
					//Password found
					found = true;
					//printf("Password Found\n");
					unsigned char tok[] = ":";
					unsigned char new_msg[MAX_SIZE];
					memset(new_msg, 0, MAX_SIZE);
					char message[] = "f";
				
					strcat((char *)new_msg, (const char *)message);
					strcat((char *)new_msg, (const char *)tok);
					strcat((char *)new_msg, (const char *)lower);

					
				
					//Return password to the server
					lsp_client_write(worker, (uint8_t *) new_msg, strlen((const char *)new_msg));
					//lsp_client_write(worker, (uint8_t *) lower, strlen((const char *)lower));
				}		
				else
				{
					//Iterate through the passwords
					for(int a = strlen((const char *)lower)-1; a >= 0 && inc !=1; a--)
					{
						//Increment the letters
						if(lower[a] < max)
						{
							lower[a]++;
							inc = 1;
						}
						else lower[a] = min;
					}
				}
			//Do while the password is not found and the range is within the assigned workload		
			} while(strcmp((const char *)lower, (const char *)upper) < 0 && found == false);
		
			//If it was not found, return the answer to the server
			if(found == false)
			{
				//printf("Password Not Found \n");
				char message2[] = "x";
				lsp_client_write(worker, (uint8_t *) message2, strlen((const char *)message2));
			}
		}		
	//Continue to do work, while the server has not lost connection	
	} while (lsp_client_alive(worker) == true);
	
	lsp_client_close(worker);
	
	return 0;
}

